/*
 *    Copyright © OpenAtom Foundation.
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */

package com.inspur.edp.bef.dtgenerator.core.mgrImpl.mgrActionAssembler;

import com.inspur.edp.bef.bizentity.operation.internalmgraction.QueryMgrAction;
import com.inspur.edp.bef.dtgenerator.core.BefCoreGenCtx;
import com.inspur.edp.caf.generator.baseInfo.ParameterInfo;
import com.inspur.edp.caf.generator.baseInfo.TypeInfo;
import com.inspur.edp.caf.generator.method.ClassMethodGenerator;
import com.inspur.edp.cef.api.authority.AuthorityInfo;
import java.util.ArrayList;
import org.eclipse.jdt.core.dom.Block;
import org.eclipse.jdt.core.dom.Modifier;
import org.eclipse.jdt.core.dom.ReturnStatement;

//
//    public override List<AuthorityInfo>  getDataPermissionFilters()
//    {
//        var rez1 = AuthUtil.GetQueryFilters(authorizationId, authOpId, authFields, beFields);
//        var rez2 = AuthUtil.GetQueryFilters(authorizationId, authOpId, authFields, beFields);
//        rez1.AddRange(rez2);
//        return rez1;
//    }
//
public class QueryMgrActionFilterMethodGenerator extends ClassMethodGenerator {
  private static final String itemName = "GetDataPermissionFilters";
  private BefCoreGenCtx coreGenCtx;
  private QueryMgrAction action;

  public QueryMgrActionFilterMethodGenerator(BefCoreGenCtx coreGenCtx, QueryMgrAction action) {
    this.coreGenCtx = coreGenCtx;
    this.action = action;
  }

  @Override
  protected Block buildMethodBody() {
    Block block = ast.newBlock();
    ReturnStatement returnStatement = ast.newReturnStatement();
    returnStatement.setExpression(ast.newNullLiteral());
    block.statements().add(returnStatement);
    return block;
    //		//key:authorizationId/authOpId, value:authFields/beFields
    //		java.util.HashMap<Tuple<String, String>, Tuple<java.util.ArrayList<String>,
    // java.util.ArrayList<String>>> filterPars = new java.util.HashMap<Tuple<String, String>,
    // Tuple<java.util.ArrayList<String>, java.util.ArrayList<String>>>();
//		for (var auField : coreGenCtx.getBeGenCtx().getBE().getMainObject().AuthFieldInfos)
    //		{
// keyword - these cannot be converted using the 'RefObject' helper class unless the method is
    // within the code being modified:
    //			if (!auField.ActionInfos.TryGetValue(action.ID, out var actionInfo))
    //			{
    //				continue;
    //			}
    //			Tuple<String, String> filterParKey = new Tuple<String, String>(auField.AuthorizationID,
    // actionInfo.OperationID);
    //			if (!((var filterParValue = filterPars.get(filterParKey)) != null))
    //			{
    //				filterPars.put(filterParKey, filterParValue = new Tuple<java.util.ArrayList<String>,
    // java.util.ArrayList<String>>(new java.util.ArrayList<String>(), new
    // java.util.ArrayList<String>()));
    //			}
    //			filterParValue.Item1.Add(auField.AuthFieldID);
    //
    //	filterParValue.Item2.Add(coreGenCtx.getBeGenCtx().getBE().getMainObject().FindElement(auField.ElementID).LabelID);
    //		}
    //		if (filterPars.isEmpty())
    //		{
    //			return
    // SyntaxFactory.Block(SyntaxFactory.<StatementSyntax>SingletonList(SyntaxFactory.ReturnStatement(SyntaxFactory.LiteralExpression(SyntaxKind.NullLiteralExpression))));
    //		}
    //		java.util.ArrayList<StatementSyntax> syntaxList = new
    // java.util.ArrayList<StatementSyntax>();
    //		int rezIndex = 1;
//		for (var filterPar : filterPars)
    //		{
    //			java.util.ArrayList<SyntaxNodeOrToken> authFieldsInitializerPars = new
    // java.util.ArrayList<SyntaxNodeOrToken>();
//			for (var item : filterPar.getValue().Item1)
    //			{
    //				if (authFieldsInitializerPars.Any())
    //				{
    //					authFieldsInitializerPars.add(SyntaxFactory.Token(SyntaxKind.CommaToken));
    //				}
    //
    //	authFieldsInitializerPars.add(SyntaxFactory.LiteralExpression(SyntaxKind.StringLiteralExpression, SyntaxFactory.Literal(item)));
    //			}
//			var createAuthFields =
    // SyntaxFactory.ObjectCreationExpression(TypeUtils.GetTypeSyntax(java.util.ArrayList<String>.class)).WithArgumentList(SyntaxFactory.ArgumentList(SyntaxFactory.<ArgumentSyntax>SingletonSeparatedList(SyntaxFactory.Argument(SyntaxFactory.LiteralExpression(SyntaxKind.NumericLiteralExpression, SyntaxFactory.Literal(filterPar.getValue().Item1.size())))))).WithInitializer(SyntaxFactory.InitializerExpression(SyntaxKind.CollectionInitializerExpression, SyntaxFactory.<ExpressionSyntax>SeparatedList(authFieldsInitializerPars.toArray(new SyntaxNodeOrToken[]{}))));
    //
    //
    //			java.util.ArrayList<SyntaxNodeOrToken> beFieldsInitializerPars = new
    // java.util.ArrayList<SyntaxNodeOrToken>();
//			for (var item : filterPar.getValue().Item2)
    //			{
    //				if (beFieldsInitializerPars.Any())
    //				{
    //					beFieldsInitializerPars.add(SyntaxFactory.Token(SyntaxKind.CommaToken));
    //				}
    //
    //	beFieldsInitializerPars.add(SyntaxFactory.LiteralExpression(SyntaxKind.StringLiteralExpression, SyntaxFactory.Literal(item)));
    //			}
//			var createBeAuthFields =
    // SyntaxFactory.ObjectCreationExpression(TypeUtils.GetTypeSyntax(java.util.ArrayList<String>.class)).WithArgumentList(SyntaxFactory.ArgumentList(SyntaxFactory.<ArgumentSyntax>SingletonSeparatedList(SyntaxFactory.Argument(SyntaxFactory.LiteralExpression(SyntaxKind.NumericLiteralExpression, SyntaxFactory.Literal(filterPar.getValue().Item2.size())))))).WithInitializer(SyntaxFactory.InitializerExpression(SyntaxKind.CollectionInitializerExpression, SyntaxFactory.<ExpressionSyntax>SeparatedList(beFieldsInitializerPars.toArray(new SyntaxNodeOrToken[]{}))));
    //
    //
    //	syntaxList.add(SyntaxFactory.LocalDeclarationStatement(SyntaxFactory.VariableDeclaration(SyntaxFactory.IdentifierName("var")).WithVariables(SyntaxFactory.<VariableDeclaratorSyntax>SingletonSeparatedList(SyntaxFactory.VariableDeclarator(SyntaxFactory.Identifier("rez" + rezIndex++)).WithInitializer(SyntaxFactory.EqualsValueClause(SyntaxFactory.InvocationExpression(SyntaxFactory.MemberAccessExpression(SyntaxKind.SimpleMemberAccessExpression, TypeUtils.GetTypeSyntax(AuthUtil.class), SyntaxFactory.IdentifierName("GetQueryFilters"))).WithArgumentList(SyntaxFactory.ArgumentList(SyntaxFactory.<ArgumentSyntax>SeparatedList(new SyntaxNodeOrToken[]{ SyntaxFactory.Argument(SyntaxFactory.LiteralExpression(SyntaxKind.StringLiteralExpression, SyntaxFactory.Literal(filterPar.getKey().Item1))), SyntaxFactory.Token(SyntaxKind.CommaToken), SyntaxFactory.Argument(SyntaxFactory.LiteralExpression(SyntaxKind.StringLiteralExpression, SyntaxFactory.Literal(filterPar.getKey().Item2))), SyntaxFactory.Token(SyntaxKind.CommaToken), SyntaxFactory.Argument(createAuthFields), SyntaxFactory.Token(SyntaxKind.CommaToken), SyntaxFactory.Argument(createBeAuthFields)})))))))));
    //		}
    //		for (int i = 2; i < rezIndex; i++)
    //		{
    //
    //	syntaxList.add(SyntaxFactory.ExpressionStatement(SyntaxFactory.InvocationExpression(SyntaxFactory.MemberAccessExpression(SyntaxKind.SimpleMemberAccessExpression, SyntaxFactory.IdentifierName("rez1"), SyntaxFactory.IdentifierName("AddRange"))).WithArgumentList(SyntaxFactory.ArgumentList(SyntaxFactory.<ArgumentSyntax>SingletonSeparatedList(SyntaxFactory.Argument(SyntaxFactory.IdentifierName("rez" + i)))))));
    //		}
    //		syntaxList.add(SyntaxFactory.ReturnStatement(SyntaxFactory.IdentifierName("rez1")));
    //		return SyntaxFactory.Block(syntaxList);
  }

  @Override
  protected boolean getIsOverride() {
    return true;
  }

  @Override
  protected java.util.ArrayList<Modifier.ModifierKeyword> getAccessModifiers() {
    return new java.util.ArrayList<Modifier.ModifierKeyword>(
        java.util.Arrays.asList(
            Modifier.ModifierKeyword.PUBLIC_KEYWORD));
  }

  @Override
  protected String getMethodName() {
    return itemName;
  }

  @Override
  protected java.util.ArrayList<ParameterInfo> getParameterCollection() {
    return new java.util.ArrayList<ParameterInfo>(1);
  }

  @Override
  protected TypeInfo getReturnType() {
    TypeInfo typeInfo = new TypeInfo(ArrayList.class.getTypeName());
    typeInfo.setIsParameterizedType(true);
    typeInfo.getArguments().add(new TypeInfo(AuthorityInfo.class.getTypeName()));
    return typeInfo;
  }
}
